home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-18 | 3.6 KB | 135 lines | [TEXT/PJMM] |
- {CheapSound2: was once a minimal sound playing unit, but is here expanded to}
- {do stereo panning too. Made by Ingemar Ragnemalm 1995.}
-
- {Notes on performance:}
- {The sound playing can be made much faster by not disposing and re-allocating the}
- {channels all the time, but rather flushing them. Also, the sounds could be permanently}
- {accessed through locked handles, not by GetResource calls. Multi-channel sound isn't}
- {in here either, and is really easy: just add more channels.}
- {All that can be fixed, but this is made to be small and safe.}
-
- unit CheapSound2;
- interface
- uses
- {$IFC UNDEFINED THINK_PASCAL}
- Types, Memory, Packages, Quickdraw, ToolUtils, GestaltEqu, Resources,
- {$ENDC}
- Sound;
- procedure TerminateSound;
- procedure PlaySound (mySndID: Integer; h, v: Integer);
- procedure PlayNamedSound (name: Str255; h, v: Integer);
-
- implementation
-
- var
- mySndChan: SndChannelPtr;
- initialized: Boolean;
- hasSound: Boolean;
- hasStereo: Boolean;
- hasSoundMgr3: Boolean;
-
- {If you have Universal Interfaces for Think Pascal, you may have to comment out this:}
- {$IFC UNDEFINED THINK_PASCAL}
- {$ELSEC}
- type
- SndListHandle = Handle;
- {$ENDC}
-
- const
- kAsync = true;
- volumeCmd = 46;
-
- procedure InitCheapSound;
- var
- err: OSErr;
- response: Longint;
- begin
- initialized := true;
- {Is sound mgr around at all? Tested through the system version.}
- err := Gestalt(gestaltSystemVersion, response);
- if err = noErr then
- hasSound := response >= $602;
- if hasSound then
- begin
- {Do we have stereo?}
- err := Gestalt(gestaltSoundAttr, response);
- if err = noErr then
- hasStereo := BitAnd(response, BSL(1, gestaltStereoCapability)) <> 0;
- {Do we have modern sound mgr?}
- hasSoundMgr3 := SndSoundManagerVersion.majorRev >= 3;
- end;
- end; {InitCheapSound}
-
- procedure TerminateSound;
- var
- myErr: OSErr;
- begin
- if not initialized then
- InitCheapSound;
- if not hasSound then
- Exit(TerminateSound);
-
- if mySndChan <> nil then
- myErr := SndDisposeChannel(mySndChan, TRUE);
- mySndChan := nil;
- end; {TerminateSound}
-
- procedure PlaySoundHandle (mySndHandle: Handle; h, v: Integer);
- var
- myErr: OSErr;
- pos: Point;
- theCmd: SndCommand;
- begin
- TerminateSound;
- if mySndHandle = nil then
- Exit(PlaySoundHandle);
- if not hasSound then
- Exit(PlaySoundHandle);
-
- mySndChan := nil;
-
- {If stereo isn't available or stereo panning is possible, initialize standard (0),}
- {else initialize either right or left as appropriate.}
- if (hasStereo and hasSoundMgr3) or not hasStereo or (Abs(h - v) < 100) then
- myErr := SndNewChannel(mySndChan, sampledSynth, 0, nil)
- else if h > v then
- myErr := SndNewChannel(mySndChan, sampledSynth, initChanRight, nil)
- else
- myErr := SndNewChannel(mySndChan, sampledSynth, initChanLeft, nil);
-
- if hasStereo and hasSoundMgr3 then
- begin
- theCmd.cmd := volumeCmd;
- theCmd.param1 := 0;
- pos.h := h;
- pos.v := v;
- theCmd.param2 := Longint(pos);
- myErr := SndDoImmediate(mySndChan, theCmd);
- end;
-
- myErr := SndPlay(mySndChan, SndListHandle(mySndHandle), kAsync);
- end; {PlaySoundHandle}
-
- procedure PlaySound (mySndID: Integer; h, v: Integer);
- var
- mySndHandle: Handle;
- begin
- mySndHandle := GetResource('snd ', mySndID);
- if mySndHandle = nil then
- Exit(PlaySound);
- HLock(mySndHandle);
- PlaySoundHandle(mySndHandle, h, v);
- end; {PlaySound}
-
- procedure PlayNamedSound (name: Str255; h, v: Integer);
- var
- mySndHandle: Handle;
- begin
- mySndHandle := GetNamedResource('snd ', name);
- if mySndHandle = nil then
- Exit(PlayNamedSound);
- HLock(mySndHandle);
- PlaySoundHandle(mySndHandle, h, v);
- end; {PlayNamedSound}
-
- end.